package service

import (
	"encoding/json"

	"io/ioutil"
	"net/http"
	"time"

	"crossrun.top/control"
	"crossrun.top/model"

	simplejson "github.com/bitly/go-simplejson"
)

func SaveBlog(w http.ResponseWriter, r *http.Request) {
	var res model.ResPonse
	b, _ := ioutil.ReadAll(r.Body) //把  body 内容读入 s
	var f interface{}
	e := json.Unmarshal(b, &f)
	if e != nil {
		res.ResCode = "0"
		res.ResMessage = e.Error()
		data, _ := json.Marshal(res)
		w.Write(data)
		return
	}
	m := f.(map[string]interface{})

	var blog model.Blog
	title, ok := m["title"].(string)
	if ok {
		blog.Title = title
	}
	content, ok := m["content"].(string)
	if ok {
		blog.Content = content
	}

	blog.Time = time.Now().UnixNano() / 1e6
	blog.Modification = blog.Time
	cid, ok := m["cid"].(string)
	if ok {
		blog.Cata = model.Catalog{"", cid, 1}
	}

	if blog.Title == "" {
		res.ResCode = "0"
		res.ResMessage = "缺少参数title"
		data, _ := json.Marshal(res)
		w.Write(data)
		return
	}
	if blog.Content == "" {
		res.ResCode = "0"
		res.ResMessage = "缺少参数content"
		data, _ := json.Marshal(res)
		w.Write(data)
		return
	}
	if blog.Cata.Id == "" {
		res.ResCode = "0"
		res.ResMessage = "缺少参数cid"
		data, _ := json.Marshal(res)
		w.Write(data)
		return
	}

	err := control.AddBlog(blog)

	if err != nil {
		res.ResCode = "0"
		res.ResMessage = err.Error()
	} else {
		res.ResCode = "1"
		res.ResMessage = "添加成功"
	}
	data, _ := json.Marshal(res)
	w.Write(data)
}

func ListBlog(w http.ResponseWriter, r *http.Request) {
	var resp model.ResPonse
	b, _ := ioutil.ReadAll(r.Body) //把  body 内容读入 b
	res, err := simplejson.NewJson(b)
	if err != nil {
		resp.ResCode = "0"
		resp.ResMessage = err.Error()
		data, _ := json.Marshal(resp)
		w.Write(data)
		return
	}
	pageNum, ok := res.Get("pageNum").Int()
	if ok != nil {
		resp.ResCode = "0"
		resp.ResMessage = "miss pageNum"
		data, _ := json.Marshal(resp)
		w.Write(data)
		return
	}

	pageSize, ok := res.Get("pageSize").Int()
	if ok != nil {
		resp.ResCode = "0"
		resp.ResMessage = "miss pageSize"
		data, _ := json.Marshal(resp)
		w.Write(data)
		return
	}
	cid, ok := res.Get("cid").String()

	var catas []model.Blog
	if cid == "" {
		catas, err = control.ListBlog(pageNum, pageSize)
	} else {
		catas, err = control.ListBlogByCat(cid, pageNum, pageSize)
	}
	if len(catas) < 1 || err != nil {
		resp.ResCode = "0"
	} else {
		var blogRes model.ListResPonse
		blogRes.PageNum = pageNum
		blogRes.TotalNum, err = control.ListBlogTotalSize(pageSize, cid)
		if err != nil {
			resp.ResCode = "0"
		} else {
			blogRes.Res = catas
			resp.ResCode = "1"
			resp.Res = blogRes
		}
	}
	data, _ := json.Marshal(resp)
	w.Write(data)
}

func Blog(w http.ResponseWriter, r *http.Request) {
	var resp model.ResPonse
	b, _ := ioutil.ReadAll(r.Body) //把  body 内容读入 b
	res, err := simplejson.NewJson(b)
	if err != nil {
		resp.ResCode = "0"
		resp.ResMessage = err.Error()
		data, _ := json.Marshal(resp)
		w.Write(data)
		return
	}
	id, ok := res.Get("id").String()
	if ok != nil {
		resp.ResCode = "0"
		resp.ResMessage = "miss id"
		data, _ := json.Marshal(resp)
		w.Write(data)
		return
	}
	blog := control.ListBlogById(id)
	if blog.Id == "" {
		resp.ResCode = "0"
	} else {
		resp.ResCode = "1"
		resp.Res = blog
	}
	data, _ := json.Marshal(resp)
	w.Write(data)
}
